/*
 * Copyright 2016 Freelander
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.handong.handserialprogram.module.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.utils.JLog;

import java.util.Calendar;


/**
 * Created by Jun on 2016/5/2.
 */
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements View.OnClickListener {

    private static final String TAG = "BaseActivity";
    private static final int DOUBLE_CLICK_SPACE = 800;

    protected View mContentView;
    protected P mPresenter;

    private long mTimerCounter = 0;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mPresenter = createPresenter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JLog.d(TAG, this.getClass().getSimpleName() + " onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mContentView = inflater.inflate(getContentViewID(), container, false);
        initViewByID(mContentView);
        initViewData();
        JLog.d(TAG, this.getClass().getSimpleName() + " onCreateView");
        return mContentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        JLog.d(TAG, this.getClass().getSimpleName() + " onViewCreated");
    }

    @Override
    public void onResume() {
        super.onResume();
        JLog.d(TAG, this.getClass().getSimpleName() + " onResume");
    }

    protected abstract P createPresenter();

    protected abstract void initViewByID(View view);

    protected abstract void initViewData();

    protected int getContentViewID() {
        return 0;
    }

    public View getContentView() {
        return mContentView;
    }

    public View findViewById(int resId) {
        return mContentView.findViewById(resId);
    }

    @Override
    public void onClick(View v) {
    }

    protected boolean dispatchDoubleClick(){
        long now = Calendar.getInstance().getTimeInMillis();
        if (now - mTimerCounter < DOUBLE_CLICK_SPACE) {
            JLog.d("DOUBLE_CLICK_SPACE", "time is short.");
            return true;
        }
        mTimerCounter = now;
        return false;
    }

    protected void showShortToast(String pMsg) {
        FragmentActivity fragmentActivity=getActivity();
        if (fragmentActivity!=null){
            Toast.makeText(getActivity(), pMsg, Toast.LENGTH_SHORT).show();
        }

    }

    public void openActivity(Class<? extends BaseActivity> toActivity) {
        openActivity(toActivity, null);
    }

    public void openActivity(Class<? extends BaseActivity> toActivity, Bundle parameter) {
        Intent intent = new Intent(getActivity(), toActivity);
        if (parameter != null) {
            intent.putExtras(parameter);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.anim_no);
    }

    public void showLoadDialog(String content) {
        BaseActivity activity = (BaseActivity) getActivity();
        activity.showLoadDialog(content);
    }

    public void dismissLoadingDialog() {
        BaseActivity activity = (BaseActivity) getActivity();
        activity.dismissLoadingDialog();
    }

    @Override
    public void onStart() {
        super.onStart();
        JLog.d(TAG, this.getClass().getSimpleName() + " onStart");
    }

    @Override
    public void onPause() {
        super.onPause();
        JLog.d(TAG, this.getClass().getSimpleName() + " onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        JLog.d(TAG, this.getClass().getSimpleName() + " onStop");
    }

    @Override
    public void onDestroyView() {
        if (mPresenter != null) {
            mPresenter.exit();
            mPresenter = null;
        }
        if (mContentView != null) {
            mContentView = null;
        }
        mTimerCounter = 0;
        super.onDestroyView();
        JLog.d(TAG, this.getClass().getSimpleName() + " onDestroyView");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        JLog.d(TAG, this.getClass().getSimpleName() + " onDetach");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        JLog.d(TAG, this.getClass().getSimpleName() + " onDestroy");
    }
}
